<template>
  <el-dialog
    class="ba-operate-dialog"
    :close-on-click-modal="false"
    :model-value="baTable.form.operate ? true : false"
    @close="baTable.toggleForm"
  >
    <template #header>
      <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']">
        晨会问题跟踪
      </div>
    </template>
    <el-scrollbar
      v-loading="baTable.form.loading"
      class="ba-table-form-scrollbar"
    >
      <div
        class="ba-operate-form"
        :class="'ba-' + baTable.form.operate + '-form'"
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
      >
        <el-form
          ref="formRef"
          @keyup.enter="onAddSubmit(formRef)"
          :model="state.form"
          label-position="right"
          label-width="160px"
          :rules="rules"
        >
          <el-form-item label="日期">
            <el-date-picker
              v-model="state.form.createTime"
              type="date"
              placeholder="请选择日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD "
            />
          </el-form-item>
          <el-form-item label="料号" prop="goodsNo">
            <el-input v-model="state.form.goodsNo" disabled />
          </el-form-item>
          <el-form-item label="问题域" prop="problem">
            <el-select
              v-model="state.form.problem"
              placeholder="请选择机柜类型"
            >
              <el-option
                v-for="item in processPull.type"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictLabel"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="晨会问题">
            <el-input
              v-model="state.form.more"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 5 }"
            ></el-input>
          </el-form-item>
          <el-form-item label="处理描述">
            <el-input
              v-model="state.form.remark"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 5 }"
            ></el-input>
          </el-form-item>
          <FormItem
            label="问题状态"
            v-model="state.form.status"
            type="radio"
            prop="status"
            :data="{
              content: processPull.tags.status,
              childrenAttr: { border: true },
            }"
          />
          <el-form-item label="处理进度%" prop="progress">
            <el-input-number
              v-model="state.form.progress"
              :min="10"
              :max="100"
              :precision="0"
            />
          </el-form-item>
          <el-form-item label="处理人员" prop="userId">
            <el-select v-model="state.form.userId" placeholder="请选择机柜类型">
              <el-option
                v-for="item in state.userOptions"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              >
                <span style="float: left">{{ item.userName }}</span>
                <span
                  style="
                    float: right;
                    color: var(--el-text-color-secondary);
                    font-size: 13px;
                  "
                >
                  {{ item.nickName }}
                </span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工单" prop="workOrder">
            <el-input v-model="state.form.workOrder" />
          </el-form-item>
          <el-form-item label="工作中心" prop="workCenter">
            <el-input v-model="state.form.workCenter" />
          </el-form-item>
          <el-form-item label="机台" prop="workCenter">
            <el-input v-model="state.form.workCenter" />
          </el-form-item>
          <el-form-item label="部门" prop="deptId">
            <el-tree-select
              v-model="state.form.deptId"
              :data="state.deptOptions"
              :props="{
                label: 'label',
                value: 'id',
                children: 'children',
              }"
              :render-after-expand="false"
              check-strictly
            />
          </el-form-item>
          <!-- <el-form-item label="站点" prop="siteId">
            <el-tree-select
              v-model="baTable.form.items!.siteId"
              :data="state.siteOptions"
              node-key="siteId"
              check-strictly
              :props="{
                label: 'siteName',
                value: 'siteId',
                children: 'children',
              }"
              :render-after-expand="false"
              @node-click="siteChange"
            />
          </el-form-item>
          <el-form-item label="告警策略" prop="warningName">
            <el-select
              v-model="baTable.form.items!.warningName"
              @clear="onClear"
              value-key="id"
              @change="selectWarning"
            >
              <el-option
                v-for="(item, index) in state.warningOptions"
                :label="item.name"
                :value="item"
              />
            </el-select>
          </el-form-item>
          -->
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'"
      >
        <el-button @click="baTable.toggleForm('')">取消</el-button>
        <el-button
          v-blur
          :loading="baTable.form.submitLoading"
          @click="baTable.onSubmit(formRef)"
          type="primary"
        >
          {{ baTable.form.operate === "itemAdd" ? "确定并新增下一项" : "确定" }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import _ from "lodash";
import { reactive, ref, inject, onMounted, watch, onBeforeUnmount } from "vue";
import type baTableClass from "@/utils/baTable";
import { useConfig } from "@/store/config"; //后台布局相关仓库
import type {
  ElForm,
  FormItemRule,
  FormRules,
  ElTree,
  FormInstance,
} from "element-plus";

type Option = {
  id: number;
  label: string;
  children: Option[];
};

const filterText = ref("");
const treeRefSupplier = ref<InstanceType<typeof ElTree>>();
//接受来自父组件的注入
const baTable = inject("baTable") as baTableClass;
const dictionary = inject("dictionary") as any;
const processPull = inject("processPull") as any;
const dept = inject("dept") as any;
const user = inject("user") as any;
const dictOperation = inject("dictOperation") as any;
const formRef = ref<InstanceType<typeof ElForm>>();

const defaultProps = {
  children: "children",
  label: "deptName",
  value: "id",
};
const defaultPropsSupplier = {
  children: "children",
  label: "supplierName",
  value: "supplierId",
};

const state: {
  siteOptions: Option[];
  warningOptions: anyObj[];
  deptOptions: any[];
  userOptions: any[];
  form: any;
} = reactive({
  siteOptions: [], //站点列表
  warningOptions: [], //告警策略列表
  deptOptions: [], //部门列表
  userOptions: [], //用户列表
  form: {},
});
const rules = reactive<FormRules>({
  cabinetName: [
    { required: true, message: "机柜名称不能为空", trigger: "blur" },
  ],
  cabinetType: [
    { required: true, message: "机柜类型不能为空", trigger: "blur" },
  ],
  siteId: [{ required: true, message: "站点不能为空", trigger: "blur" }],
  emergencyPhone: [
    {
      required: true,
      message: "应急电话不能为空",
      trigger: "blur",
    },
    {
      validator: function (rule, value, callback) {
        if (/^1[34578]\d{9}$/.test(value) == false) {
          callback(new Error("格式错误"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
    // { type: "string", message: "长度不能超过30位", trigger: "blur", max: 30 },
  ],
});

const onAddSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: any, fields: any) => {
    if (valid) {
    } else {
      console.log("error submit!", fields);
    }
  });
};

//获取部门数据列表
const getDeptList = () => {
  dept.getDept().then((res: any) => {
    state.deptOptions = res.data;
    console.log("state.deptOptions", state.deptOptions);
  });
};
//获取用户数据列表
const getUserList = () => {
  user.getUsers({ pageSize: 999999 }).then((res: any) => {
    state.userOptions = res.rows;
  });
};

onMounted(() => {
  //获取部门数据列表
  getDeptList();
  //获取用户数据列表
  getUserList();
  setTimeout(() => {
    //新增晨会问题跟踪表单 料号数据
    state.form.goodsNo = baTable.form.extend!.problem.goodsNo;
  }, 500);
});

watch(filterText, (val) => {
  treeRefSupplier.value!.filter(val);
});
defineExpose({});

onBeforeUnmount(() => {});
</script>
<style lang="scss" scoped>
.el-scrollbar {
  height: 500px;
  overflow-y: auto;
}

.vertical-menus-scrollbar {
  border: 1px solid #e5e6e7;
}
.tree-border {
  width: 100%;
  margin-top: 5px;
  // background: #fff none;
  border-radius: 4px;
}
</style>
